"""
LLM相关Schema定义
"""
from typing import List, Optional, Dict, Any
from pydantic import BaseModel, Field
from datetime import datetime


class LLMModelCreate(BaseModel):
    """创建LLM模型配置"""
    
    name: str = Field(..., description="模型名称")
    provider: str = Field(..., description="提供商")
    llm_model_id: str = Field(..., description="模型标识符")
    api_key: str = Field(..., description="API密钥")
    base_url: Optional[str] = Field(None, description="基础URL")
    max_tokens: int = Field(4000, description="最大token数")
    temperature: float = Field(0.7, description="温度参数", ge=0, le=2)
    is_active: bool = Field(True, description="是否激活")


class LLMModelResponse(BaseModel):
    """LLM模型响应"""
    
    model_config = {"from_attributes": True}
    
    id: int
    name: str
    provider: str
    llm_model_id: str
    max_tokens: int
    temperature: float
    is_active: bool
    created_at: datetime
    updated_at: Optional[datetime]


class ChatMessageCreate(BaseModel):
    """创建消息"""
    conversation_id: int = Field(..., description="对话ID")
    role: str = Field(..., description="角色", pattern="^(user|assistant)$")
    content: str = Field(..., description="消息内容")


class ChatMessageResponse(BaseModel):
    """消息响应"""
    
    model_config = {"from_attributes": True}
    
    id: int
    conversation_id: int
    role: str
    content: str
    created_at: datetime


class ChatConversationCreate(BaseModel):
    """创建对话"""
    
    model_config = {}
    
    title: str = Field(..., description="对话标题", max_length=200)
    llm_model_id: int = Field(..., description="模型ID")


class ChatConversationResponse(BaseModel):
    """对话响应"""
    
    model_config = {"from_attributes": True}
    
    id: int
    user_id: int
    title: str
    llm_model_id: int
    llm_model_name: str
    provider: str
    is_active: bool
    created_at: datetime
    updated_at: Optional[datetime]
    message_count: int = 0


class ChatRequest(BaseModel):
    """聊天请求"""
    conversation_id: int = Field(..., description="对话ID")
    message: str = Field(..., description="用户消息")
    stream: bool = Field(False, description="是否流式响应")


class ChatResponse(BaseModel):
    """聊天响应"""
    message: str = Field(..., description="AI响应内容")
    conversation_id: int
    message_id: int


class ChatStreamResponse(BaseModel):
    """流式聊天响应"""
    content: str = Field(..., description="响应内容块")
    is_complete: bool = Field(False, description="是否完成")


class ConversationHistoryResponse(BaseModel):
    """对话历史响应"""
    conversation: ChatConversationResponse
    messages: List[ChatMessageResponse]


class LLMProviderInfo(BaseModel):
    """LLM提供商信息"""
    provider: str
    name: str
    description: str
    models: List[str]
    is_available: bool


class TokenUsageResponse(BaseModel):
    """Token使用量响应"""
    conversation_id: int
    total_tokens: int
    prompt_tokens: int
    completion_tokens: int
    estimated_cost: float = 0.0